// components/floatingFrame/index.js
const app = getApp();
var rootDocment = app.globalData.postUrl;
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Component({

  /**
   * 页面的初始数据
   */
  properties: {
    meIsShow: {
      type: Boolean,
      value: true
    },
  },

  data: {
    defHead: defaultAvatarUrl,
    avatarUrl: '',
    nickname: '',
    isShow: true,
    bottomHeight: ''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  created() {
    console.log(app.globalData.bottomHeight, "_+_+_+_+_+_++-");
    this.setData({
      bottomHeight: app.globalData.bottomHeight,
      isShow: false
    })
  },
  onShow() {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 微信头像获取
    onChooseAvatar(e) {
      const {
        avatarUrl
      } = e.detail
      this.setData({
        defHead: avatarUrl,
      })
      wx.uploadFile({
        url: rootDocment + '/api/user/z_upload',
        name: 'up_file',
        filePath: avatarUrl,
        header: {},
        formData: {
          up_file: this.data.avatarUrl,
        },
        success: (res) => {
          var paraArr = new Array();
          var sign = app.signature(paraArr);
          const url = JSON.parse(res.data)
          console.log(url)
          this.setData({
            avatarUrl: url.data.image_url
          })
          wx.setStorageSync('userImage', url.data.image_url)


        }
      })
    },
    /**
     * 提交修改
     */
    formSubmit(e) {
      let that = this
      console.log(e, 'userinfo');
      if (!that.data.avatarUrl) {
        wx.showToast({
          title: '请上传头像',
          icon: 'none'
        })
        return
      }
      if (!e.detail.value.nickname) {
        wx.showToast({
          title: '请输入昵称',
          icon: 'none'
        })
        return
      }


      this.setData({
        nickname: e.detail.value.nickname
      })


      wx.request({
        url: rootDocment + '/api/user/z_save_nickname',
        method: 'POST',
        data: {
          id: wx.getStorageSync('user_id3'),
          nickname: e.detail.value.nickname,

        },
        success(val) {
          console.log(val, '昵称')
        }
      })
      wx.request({
        url: rootDocment + '/api/user/z_save_avatar',
        method: "POST",
        data: {
          avatar: that.data.avatarUrl,
          id: wx.getStorageSync('user_id3'),
        },
        success(res) {
          that.triggerEvent('myevent')
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
          that.setData({
            isShow: true
          })
          wx.setStorageSync('meIsShow', true)
        }
      })
    },
    closeAttr() {
      let that = this
      console.log('_+_+_+_+_+_');
      that.setData({
        isShow: true
      })
    },

  },
  observers: {
    meIsShow: function (newVal) {
      this.setData({
        isShow: newVal
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})